/*
#          .,:,,,                                        .::,,,::.          
#        .::::,,;;,                                  .,;;:,,....:i:         
#        :i,.::::,;i:.      ....,,:::::::::,....   .;i:,.  ......;i.        
#        :;..:::;::::i;,,:::;:,,,,,,,,,,..,.,,:::iri:. .,:irsr:,.;i.        
#        ;;..,::::;;;;ri,,,.                    ..,,:;s1s1ssrr;,.;r,        
#        :;. ,::;ii;:,     . ...................     .;iirri;;;,,;i,        
#        ,i. .;ri:.   ... ............................  .,,:;:,,,;i:        
#        :s,.;r:... ....................................... .::;::s;        
#        ,1r::. .............,,,.,,:,,........................,;iir;        
#        ,s;...........     ..::.,;:,,.          ...............,;1s        
#       :i,..,.              .,:,,::,.          .......... .......;1,       
#      ir,....:rrssr;:,       ,,.,::.     .r5S9989398G95hr;. ....,.:s,      
#     ;r,..,s9855513XHAG3i   .,,,,,,,.  ,S931,.,,.;s;s&BHHA8s.,..,..:r:     
#    :r;..rGGh,  :SAG;;G@BS:.,,,,,,,,,.r83:      hHH1sXMBHHHM3..,,,,.ir.    
#   ,si,.1GS,   sBMAAX&MBMB5,,,,,,:,,.:&8       3@HXHBMBHBBH#X,.,,,,,,rr    
#   ;1:,,SH:   .A@&&B#&8H#BS,,,,,,,,,.,5XS,     3@MHABM&59M#As..,,,,:,is,   
#  .rr,,,;9&1   hBHHBB&8AMGr,,,,,,,,,,,:h&&9s;   r9&BMHBHMB9:  . .,,,,;ri.  
#  :1:....:5&XSi;r8BMBHHA9r:,......,,,,:ii19GG88899XHHH&GSr.      ...,:rs.  
#  ;s.     .:sS8G8GG889hi.        ....,,:;:,.:irssrriii:,.        ...,,i1,  
#  ;1,         ..,....,,isssi;,        .,,.                      ....,.i1,  
#  ;h:               i9HHBMBBHAX9:         .                     ...,,,rs,  
#  ,1i..            :A#MBBBBMHB #  .r1,..        ,..;3BMBBBHBB#Bh.     ..                    ....,,,,,i1;   
#   :h;..       .,..;,1XBMMMMBXs,.,, .. :: ,.               ....,,,,,,ss.   
#    ih: ..    .;;;, ;;:s58A3i,..    ,. ,.:,,.             ...,,,,,:,s1,    
#    .s1,....   .,;sh,  ,iSAXs;.    ,.  ,,.i85            ...,,,,,,:i1;     
#     .rh: ...     rXG9XBBM#M#MHAX3hss13&&HHXr         .....,,,,,,,ih;      
#      .s5: .....    i598X&&A&AAAAAA&XG851r:       ........,,,,:,,sh;       
#      . ihr, ...  .         ..                    ........,,,,,;11:.       
#         ,s1i. ...  ..,,,..,,,.,,.,,.,..       ........,,.,,.;s5i.         
#          .:s1r,......................       ..............;shs,           
#          . .:shr:.  ....                 ..............,ishs.             
#              .,issr;,... ...........................,is1s;.               
#                 .,is1si;:,....................,:;ir1sr;,                  
#                    ..:isssssrrii;::::::;;iirsssssr;:..                    
#                         .,::iiirsssssssssrri;;:.
*/
package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckItemMapper;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * <dl>
 * <dd>描述: ~ CheckItemServiceImpl</dd>
 * <dd>创建时间：  10:15 2021/2/24</dd>
 * <dd>创建人： guodong</dd>
 * <dt>版本历史: </dt>
 * <pre>
 * Date         Author         Version     Description
 * ------------------------------------------------------------------
 * 2021/2/24      guodong         1.0        1.0 Version
 * </pre>
 * </dl>
 */
@DubboService
@Slf4j
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemMapper checkItemMapper;

    /**
     * @Author: guodong
     * @Date: 10:16 2021/2/24
     * @Parms [queryPageBean]
     * @ReturnType: com.itheima.health.entity.PageResult
     * @Description: 检查项---分页查询
    */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        //1.分页查询
        /*

            SELECT
                *
            FROM
                `t_checkitem`

            LIMIT 10,10;

            -- int start = (pageNum-1)*pageSize
            -- (2-1)*10


            第一个参数： 当前页
            第二个参数： 页大小
            无需换算
         */
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //2.获取查询条件判空
        String queryString = queryPageBean.getQueryString();
        //查询条件对象
        Example example = new Example(CheckItem.class);
        //Criteria 是Example 内部类
        Example.Criteria criteria = example.createCriteria();

        if(!StringUtils.isEmpty(queryString)){ //不为空
            //此处注意需要手动拼接前后 百分号 不会自动拼接
            criteria.orLike("code", "%" + queryString + "%");
            criteria.orLike("name", "%" + queryString + "%");

        }
        //使用分页插件强转
        Page<CheckItem> page = (Page<CheckItem>) checkItemMapper.selectByExample(example);
        List<CheckItem> checkItemList = page.getResult(); //查询结果
        long total = page.getTotal(); //总记录数
        return new PageResult(total, checkItemList);
    }

    /**
     * @Author: guodong
     * @Date: 11:59 2021/2/24
     * @Parms [checkItem]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 新增检查项
     */
    @Override
    public void add(CheckItem checkItem) {
        log.info("[检查项---新增]data: {}", checkItem);

        /*
            我们一般使用的是带Selective的方法
            带Selective的方法会根据对象中的数据是否为null来拼接sql语句，如果为null,则不参与sql语句的拼接
            //insert into t_check_item (id,code....) values (1,"001".....)
            checkItemMapper.insertSelective(checkItem);
            不带Selective全表字段参与sql语句拼接，如果对象中没有字段那么将该值赋值为null,举例如下： name在checkItem中不存在，会自动拼接null
            //insert into t_check_item (id,name,code....) values (1,null,"001".....)
            checkItemMapper.insert(checkItem);
         */
        //insert into t_check_item (id,code....) values (1,"001".....)
        checkItemMapper.insertSelective(checkItem);
    }

    /**
     * @Author: guodong
     * @Date: 12:24 2021/2/24
     * @Parms [id]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 检查项-根据id删除
     */
    @Override
    public void deleteById(Integer id) {
        log.info("[检查项-根据id删除]data: {}", id );
        checkItemMapper.deleteByPrimaryKey(id);
    }

    /**
     * @Author: guodong
     * @Date: 14:44 2021/2/24
     * @Parms [id]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 检查项-根据id查询
     */
    @Override
    public CheckItem findById(Integer id) {
        log.info("[检查项-根据id查询]data: {}", id );
        //根据主键id查询
        return checkItemMapper.selectByPrimaryKey(id);
    }

    /**
     * @Author: guodong
     * @Date: 14:54 2021/2/24
     * @Parms [checkItem]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 检查项-根据id修改数据
     */
    @Override
    public void edit(CheckItem checkItem) {
        log.info("[检查项-根据id修改数据]data: {}", checkItem);
        checkItemMapper.updateByPrimaryKeySelective(checkItem);
    }

    /**
     * @Author: guodong
     * @Date: 14:49 2021/2/25
     * @Parms []
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 检查项-查询所有数据
     */
    @Override
    public List<CheckItem> findAll() {
        return checkItemMapper.selectAll();
    }
}
